package tw.com.msig.b2c.car.setting.web;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import tw.com.msig.b2c.car.common.web.APILibDirector;
import tw.com.msig.b2c.car.common.web.BaseAction;
import tw.com.msig.b2c.car.setting.service.CityService;
import tw.com.msig.b2c.car.setting.vo.CityVo;

public class CityAction extends BaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String forwardPage = "list";

		String sActionType = request.getParameter("sActionType");

		if ("list".equals(sActionType)) {
			forwardPage = list(request, response);
		} else if ("add".equals(sActionType)) {
			forwardPage = add(request, response);
		} else if ("edit".equals(sActionType)) {
			forwardPage = edit(request, response);
		} else if ("delete".equals(sActionType)) {
			forwardPage = delete(request, response);
		} else if ("insert".equals(sActionType)) {
			forwardPage = insert(request, response);
		} else if ("update".equals(sActionType)) {
			forwardPage = update(request, response);
		}
		return mapping.findForward(forwardPage);
	}

	private String update(HttpServletRequest request,
			HttpServletResponse response) {
		CityService cityService = (CityService) APILibDirector
				.getSpringBean("cityService");
		
		CityVo cityVo = new CityVo();
		cityVo.setId(NumberUtils.toLong(request.getParameter("id")));
		cityVo.setName(request.getParameter("name"));
		cityVo.setModifier((String)request.getSession().getAttribute("b2cUser"));
		cityVo.setModifyTime(new Timestamp(new Date().getTime()));
		
		cityService.update(cityVo);		
		
		return edit(request,response);
	}

	private String insert(HttpServletRequest request,
			HttpServletResponse response) {
		
		CityService cityService = (CityService) APILibDirector
				.getSpringBean("cityService");
		
		CityVo cityVo = new CityVo();
		cityVo.setCode(request.getParameter("code"));
		cityVo.setName(request.getParameter("name"));
		cityVo.setCreator((String)request.getSession().getAttribute("b2cUser"));
		cityVo.setCreateTime(new Timestamp(new Date().getTime()));
		
		cityService.insert(cityVo);		
		
		return "add";
	}

	private String delete(HttpServletRequest request,
			HttpServletResponse response) {

		CityService cityService = (CityService) APILibDirector
				.getSpringBean("cityService");
				
		CityVo cityVo = new CityVo();
		cityVo.setId(NumberUtils.toLong(request.getParameter("id")));

		cityService.delete(cityVo);
		
		
		return list(request,response);
	}

	private String edit(HttpServletRequest request, HttpServletResponse response) {
		CityService cityService = (CityService) APILibDirector
				.getSpringBean("cityService");
		
		Long id = NumberUtils.toLong(request.getParameter("id"));
		
		log.debug("{}",id);
		
		CityVo cityVo = cityService.getById(id);
		
		log.debug("{}",cityVo);
		
		request.setAttribute("city", cityVo);
		
		return "edit";
	}

	private String add(HttpServletRequest request, HttpServletResponse response) {
		return "add";
	}

	public String list(HttpServletRequest request, HttpServletResponse response) {			
		
		CityService cityService = (CityService) APILibDirector
				.getSpringBean("cityService");
		List<CityVo> cityVos = cityService.getAll();
		request.setAttribute("models", cityVos);
		
		return "list";
	}
}
